// pages/companyViewUser/companyViewUser.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    predictor_id: 142,
    predictorInfo: null,
    predictNum: 0,

    forecastList: [],

  },

  /** 
   * forecastPanel
   */
  initStstusList: function () {
    var that = this;

    var panelLength = that.data.forecastList.length;
    for (var i = 0; i < panelLength; i++) {
      that.data.forecastList[i].status = false;
    }
  },
  changeStatus: function (event) {
    var that = this;
    var index = event.currentTarget.dataset.index;
    var status = that.data.forecastList[index].status;
    var item = "forecastList[" + index + "].status";

    var param = {};

    if (status) {
      param[item] = false;
      that.setData(param);

    } else {
      param[item] = true;
      that.setData(param);
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取用户基本信息以及关于该公司的预测
    that.setData({
      predictor_id: options.predictor_id,
    })
    wx.request({
      url: app.globalData.server + '/user/invited/?id=' + that.data.predictor_id,
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token,
        'x-wxapp-signature': null,
        'x-wxapp-timestamp': app.getTimestamp(),
        'x-wxapp-nonce': app.getRandom()
      },
      success: function (res) {
        console.log(res);
        that.setData({
          'predictorInfo.expert_name': res.data.data.expert_name,
          'predictorInfo.expert_self_intro': res.data.data.expert_self_intro,
          'predictorInfo.expert_avatar': res.data.data.expert_avatar,
        })

        // 获取相关的预测
        wx.request({
          url: app.globalData.server + '/predict/predict/?user_id=' + that.data.predictor_id,
          method: 'GET',
          data: {},
          header: {
            'content-type': 'application/x-www-form-urlencoded', // 默认值
            'Authorization': 'Bearer ' + app.globalData.token,
            'x-wxapp-signature': null,
            'x-wxapp-timestamp': app.getTimestamp(),
            'x-wxapp-nonce': app.getRandom()
          },
          success: function (res) {
            console.log(res);
            that.setData({
              forecastList: res.data.data,
            });

            // 加入折叠标记
            that.initStstusList();
            that.setData({
              predictNum: that.data.forecastList.length,
            });
          }
        })
      }
    });





  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})